package com.concurrent.chapter3.concurrent03;

import java.util.concurrent.Phaser;

public class MyService {
	private Phaser phaser;
	public MyService(Phaser phaser) {
		super();
		this.phaser = phaser;
	}
	
	public void testMethod() {
		try {
			System.out.println("阶段1 Begin " + Thread.currentThread().getName() + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
				Thread.sleep(5000);
			}
			phaser.arriveAndAwaitAdvance();
			System.out.println("阶段1 End " + Thread.currentThread().getName() + " end phase value=" + phaser.getPhase() + "   " + System.currentTimeMillis());
			/********/
			System.out.println("阶段2 Begin " + Thread.currentThread().getName() + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
				Thread.sleep(5000);
			}
			phaser.arriveAndAwaitAdvance();
			System.out.println("阶段2 End " + Thread.currentThread().getName() + " end phase value=" + phaser.getPhase() + "   " + System.currentTimeMillis());
			/********/
			System.out.println("阶段3 Begin " + Thread.currentThread().getName() + System.currentTimeMillis());
			if (Thread.currentThread().getName().equals("B")) {
				Thread.sleep(5000);
			}
			phaser.arriveAndAwaitAdvance();
			System.out.println("阶段3 End " + Thread.currentThread().getName() + " end phase value=" + phaser.getPhase() + "   " + System.currentTimeMillis());
			/********/
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
